﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using System.Text;

namespace Chenxin.Lib.Business.BLL
{
    /// <summary>
    /// 业务逻辑类ContractApplyInfo 的摘要说明。
    /// </summary>
    public class ContractApplyInfo
    {
        public ContractApplyInfo()
        { }

        /// <summary>
        /// 业绩申请添加
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractApplyInfo model)
        {
            Chenxin.Lib.Business.DAL.ContractApplyInfo.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void  Update(Chenxin.Lib.Business.Entity.ContractApplyInfo model)
        {
            Chenxin.Lib.Business.DAL.ContractApplyInfo.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            Chenxin.Lib.Business.DAL.ContractApplyInfo.Delete(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractApplyInfo GetModel(Guid RecordId)
        {

            return Chenxin.Lib.Business.DAL.ContractApplyInfo.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Business.DAL.ContractApplyInfo.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractApplyInfo> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Business.DAL.ContractApplyInfo.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractApplyInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Business.Entity.ContractApplyInfo> modelList = new List<Chenxin.Lib.Business.Entity.ContractApplyInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Business.Entity.ContractApplyInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Business.Entity.ContractApplyInfo();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["BusinessType"].ToString() != "")
                    {
                        model.BusinessType = int.Parse(dt.Rows[n]["BusinessType"].ToString());
                    }
                    if (dt.Rows[n]["BargainType"].ToString() != "")
                    {
                        model.BargainType = int.Parse(dt.Rows[n]["BargainType"].ToString());
                    }
                    if (dt.Rows[n]["BargainPrice"].ToString() != "")
                    {
                        model.BargainPrice = decimal.Parse(dt.Rows[n]["BargainPrice"].ToString());
                    }
                    if (dt.Rows[n]["ConfirmType"].ToString() != "")
                    {
                        model.ConfirmType = int.Parse(dt.Rows[n]["ConfirmType"].ToString());
                    }
                    if (dt.Rows[n]["HouseId"].ToString() != "")
                    {
                        model.HouseId = new Guid(dt.Rows[n]["HouseId"].ToString());
                    }
                    model.HouseCode = dt.Rows[n]["HouseCode"].ToString();
                    model.HouseAddress = dt.Rows[n]["HouseAddress"].ToString();
                    if (dt.Rows[n]["HouseProperty"].ToString() != "")
                    {
                        model.HouseProperty = int.Parse(dt.Rows[n]["HouseProperty"].ToString());
                    }
                    if (dt.Rows[n]["Area"].ToString() != "")
                    {
                        model.Area = decimal.Parse(dt.Rows[n]["Area"].ToString());
                    }
                    if (dt.Rows[n]["CommunityId"].ToString() != "")
                    {
                        model.CommunityId = new Guid(dt.Rows[n]["CommunityId"].ToString());
                    }
                    model.Community = dt.Rows[n]["Community"].ToString();
                    model.CommunityAddress = dt.Rows[n]["CommunityAddress"].ToString();
                    model.PropertyCode = dt.Rows[n]["PropertyCode"].ToString();
                    model.Region = dt.Rows[n]["Region"].ToString();
                    model.Block = dt.Rows[n]["Block"].ToString();
                    if (dt.Rows[n]["CurrentFloor"].ToString() != "")
                    {
                        model.CurrentFloor = int.Parse(dt.Rows[n]["CurrentFloor"].ToString());
                    }
                    if (dt.Rows[n]["TotalFloor"].ToString() != "")
                    {
                        model.TotalFloor = int.Parse(dt.Rows[n]["TotalFloor"].ToString());
                    }
                    model.BuildingType = dt.Rows[n]["BuildingType"].ToString();
                    model.HouseUse = dt.Rows[n]["HouseUse"].ToString();
                    model.HouseType = dt.Rows[n]["HouseType"].ToString();
                    if (dt.Rows[n]["BargainCityId"].ToString() != "")
                    {
                        model.BargainCityId = new Guid(dt.Rows[n]["BargainCityId"].ToString());
                    }
                    model.BargainCityName = dt.Rows[n]["BargainCityName"].ToString();
                    if (dt.Rows[n]["BargainCompanyId"].ToString() != "")
                    {
                        model.BargainCompanyId = new Guid(dt.Rows[n]["BargainCompanyId"].ToString());
                    }
                    model.BargainCompanyName = dt.Rows[n]["BargainCompanyName"].ToString();
                    if (dt.Rows[n]["BargainDepartmentId"].ToString() != "")
                    {
                        model.BargainDepartmentId = new Guid(dt.Rows[n]["BargainDepartmentId"].ToString());
                    }
                    model.BargainDepartmentName = dt.Rows[n]["BargainDepartmentName"].ToString();
                    if (dt.Rows[n]["BargainUserId"].ToString() != "")
                    {
                        model.BargainUserId = new Guid(dt.Rows[n]["BargainUserId"].ToString());
                    }
                    model.BargainUserName = dt.Rows[n]["BargainUserName"].ToString();
                    if (dt.Rows[n]["VenderCityId"].ToString() != "")
                    {
                        model.VenderCityId = new Guid(dt.Rows[n]["VenderCityId"].ToString());
                    }
                    model.VenderCityName = dt.Rows[n]["VenderCityName"].ToString();
                    if (dt.Rows[n]["VenderCompanyId"].ToString() != "")
                    {
                        model.VenderCompanyId = new Guid(dt.Rows[n]["VenderCompanyId"].ToString());
                    }
                    model.VenderCompanyName = dt.Rows[n]["VenderCompanyName"].ToString();
                    if (dt.Rows[n]["VenderDepartId"].ToString() != "")
                    {
                        model.VenderDepartId = new Guid(dt.Rows[n]["VenderDepartId"].ToString());
                    }
                    model.VenderDepartName = dt.Rows[n]["VenderDepartName"].ToString();
                    if (dt.Rows[n]["VenderUserId"].ToString() != "")
                    {
                        model.VenderUserId = new Guid(dt.Rows[n]["VenderUserId"].ToString());
                    }
                    model.VenderUserName = dt.Rows[n]["VenderUserName"].ToString();
                    if (dt.Rows[n]["VenderId"].ToString() != "")
                    {
                        model.VenderId = new Guid(dt.Rows[n]["VenderId"].ToString());
                    }
                    model.VenderName = dt.Rows[n]["VenderName"].ToString();
                    model.VenderNo = dt.Rows[n]["VenderNo"].ToString();
                    model.VenderPhone = dt.Rows[n]["VenderPhone"].ToString();
                    model.VenderMobile = dt.Rows[n]["VenderMobile"].ToString();
                    if (dt.Rows[n]["BuyerCityId"].ToString() != "")
                    {
                        model.BuyerCityId = new Guid(dt.Rows[n]["BuyerCityId"].ToString());
                    }
                    model.BuyerCityName = dt.Rows[n]["BuyerCityName"].ToString();
                    if (dt.Rows[n]["BuyerCompanyId"].ToString() != "")
                    {
                        model.BuyerCompanyId = new Guid(dt.Rows[n]["BuyerCompanyId"].ToString());
                    }
                    model.BuyerCompanyName = dt.Rows[n]["BuyerCompanyName"].ToString();
                    if (dt.Rows[n]["BuyerDepartId"].ToString() != "")
                    {
                        model.BuyerDepartId = new Guid(dt.Rows[n]["BuyerDepartId"].ToString());
                    }
                    model.BuyerDepartName = dt.Rows[n]["BuyerDepartName"].ToString();
                    if (dt.Rows[n]["BuyerUserId"].ToString() != "")
                    {
                        model.BuyerUserId = new Guid(dt.Rows[n]["BuyerUserId"].ToString());
                    }
                    model.BuyerUserName = dt.Rows[n]["BuyerUserName"].ToString();
                    if (dt.Rows[n]["BuyerId"].ToString() != "")
                    {
                        model.BuyerId = new Guid(dt.Rows[n]["BuyerId"].ToString());
                    }
                    model.BuyerName = dt.Rows[n]["BuyerName"].ToString();
                    model.BuyerNo = dt.Rows[n]["BuyerNo"].ToString();
                    model.BuyerPhone = dt.Rows[n]["BuyerPhone"].ToString();
                    model.BuyerMobile = dt.Rows[n]["BuyerMobile"].ToString();
                    if (dt.Rows[n]["VStandardCommision"].ToString() != "")
                    {
                        model.VStandardCommision = decimal.Parse(dt.Rows[n]["VStandardCommision"].ToString());
                    }
                    if (dt.Rows[n]["BStandardCommision"].ToString() != "")
                    {
                        model.BStandardCommision = decimal.Parse(dt.Rows[n]["BStandardCommision"].ToString());
                    }
                    if (dt.Rows[n]["VRealCommision"].ToString() != "")
                    {
                        model.VRealCommision = decimal.Parse(dt.Rows[n]["VRealCommision"].ToString());
                    }
                    if (dt.Rows[n]["BRealCommision"].ToString() != "")
                    {
                        model.BRealCommision = decimal.Parse(dt.Rows[n]["BRealCommision"].ToString());
                    }
                    if (dt.Rows[n]["VCommision"].ToString() != "")
                    {
                        model.VCommision = decimal.Parse(dt.Rows[n]["VCommision"].ToString());
                    }
                    if (dt.Rows[n]["BCommision"].ToString() != "")
                    {
                        model.BCommision = decimal.Parse(dt.Rows[n]["BCommision"].ToString());
                    }
                    if (dt.Rows[n]["VConsulting"].ToString() != "")
                    {
                        model.VConsulting = decimal.Parse(dt.Rows[n]["VConsulting"].ToString());
                    }
                    if (dt.Rows[n]["BConsulting"].ToString() != "")
                    {
                        model.BConsulting = decimal.Parse(dt.Rows[n]["BConsulting"].ToString());
                    }
                    if (dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }
                    if (dt.Rows[n]["CreateCityId"].ToString() != "")
                    {
                        model.CreateCityId = new Guid(dt.Rows[n]["CreateCityId"].ToString());
                    }
                    model.CreateCityName = dt.Rows[n]["CreateCityName"].ToString();
                    if (dt.Rows[n]["CreateCompanyId"].ToString() != "")
                    {
                        model.CreateCompanyId = new Guid(dt.Rows[n]["CreateCompanyId"].ToString());
                    }
                    model.CreateCompanyName = dt.Rows[n]["CreateCompanyName"].ToString();
                    if (dt.Rows[n]["CreateDepartmentId"].ToString() != "")
                    {
                        model.CreateDepartmentId = new Guid(dt.Rows[n]["CreateDepartmentId"].ToString());
                    }
                    model.CreateDepartmentName = dt.Rows[n]["CreateDepartmentName"].ToString();
                    if (dt.Rows[n]["CreateUserId"].ToString() != "")
                    {
                        model.CreateUserId = new Guid(dt.Rows[n]["CreateUserId"].ToString());
                    }
                    model.CreateUserName = dt.Rows[n]["CreateUserName"].ToString();
                    if (dt.Rows[n]["CreateTime"].ToString() != "")
                    {
                        model.CreateTime = DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
                    }
                    if (dt.Rows[n]["UpdateCityId"].ToString() != "")
                    {
                        model.UpdateCityId = new Guid(dt.Rows[n]["UpdateCityId"].ToString());
                    }
                    model.UpdateCityName = dt.Rows[n]["UpdateCityName"].ToString();
                    if (dt.Rows[n]["UpdateCompanyId"].ToString() != "")
                    {
                        model.UpdateCompanyId = new Guid(dt.Rows[n]["UpdateCompanyId"].ToString());
                    }
                    model.UpdateCompanyName = dt.Rows[n]["UpdateCompanyName"].ToString();
                    if (dt.Rows[n]["UpdateDepartmentId"].ToString() != "")
                    {
                        model.UpdateDepartmentId = new Guid(dt.Rows[n]["UpdateDepartmentId"].ToString());
                    }
                    model.UpdateDepartmentName = dt.Rows[n]["UpdateDepartmentName"].ToString();
                    if (dt.Rows[n]["UpdateUserId"].ToString() != "")
                    {
                        model.UpdateUserId = new Guid(dt.Rows[n]["UpdateUserId"].ToString());
                    }
                    model.UpdateUserName = dt.Rows[n]["UpdateUserName"].ToString();
                    if (dt.Rows[n]["UpdateTime"].ToString() != "")
                    {
                        model.UpdateTime = DateTime.Parse(dt.Rows[n]["UpdateTime"].ToString());
                    }
                    model.BusinessDescribe = dt.Rows[n]["BusinessDescribe"].ToString();
                    if (dt.Rows[n]["ApplicationDay"].ToString() != "")
                    {
                        model.ApplicationDay = DateTime.Parse(dt.Rows[n]["ApplicationDay"].ToString());
                    }
                    if (dt.Rows[n]["DoneDay"].ToString() != "")
                    {
                        model.DoneDay = DateTime.Parse(dt.Rows[n]["DoneDay"].ToString());
                    }
                    model.BudinessCode = dt.Rows[n]["BudinessCode"].ToString();
                    model.PointRemark = dt.Rows[n]["PointRemark"].ToString();
                    model.CompleteYear = dt.Rows[n]["CompleteYear"].ToString();
                    model.Fitment = dt.Rows[n]["Fitment"].ToString();
                    model.CustomerSource = dt.Rows[n]["CustomerSource"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 查询业绩申请
        /// </summary>
        public static DataSet getList(int strPageIndex, int strPageSize, string strWhere)
        {
            //排序
            string strOrderField = " t_Contract_ApplyInfo.CreateTime desc";
            //欲选择字段列表 
            string FieldList = " RecordId, BusinessType, BargainType, BargainPrice,ctype, ConfirmType, HouseId," +
                "HouseCode, HouseAddress,HouseProperty, Area, PropertyCode, Region," +
                "Block, Community, CurrentFloor, TotalFloor, BargainCompanyId, " +
                "BargainCompanyName, VenderDepartId, VenderDepartName, VenderUserId, " +
                "VenderUserName, VenderId, VenderName, VenderNo, VenderPhone,VenderMobile, " +
                "BuyerDepartId, BuyerDepartName, BuyerUserId, BuyerUserName, BuyerId," +
                "BuyerName, BuyerNo, BuyerPhone,BuyerMobile,FixDiscount, " +
                "VRealCommision, BRealCommision, VCommision, BCommision, " +
                "VConsulting, BConsulting, Status, CreateUserId, CreateUserName, CreateTime, " +
                "BusinessDescribe, ApplicationDay, DoneDay, BudinessCode, VStandardCommision,BStandardCommision, " +
                "V_Departs,V_Users,V_Points,B_Departs,B_Users, B_Points,Departs,Users,Points,CustomerSource";

            //子查询欲选择字段列表
            string TableFieldList = " RecordId, BusinessType, BargainType, BargainPrice, "+
                "case ConfirmType when 1 then '已确认' when 0 then '未确认' end as ctype, ConfirmType, HouseId, "+
                "HouseCode, HouseAddress,HouseProperty, Area, PropertyCode, Region,FixDiscount, " +
                "Block, Community, CurrentFloor, TotalFloor, BargainCompanyId, BargainCompanyName, "+
                "VenderDepartId, VenderDepartName, VenderUserId, VenderUserName, VenderId, VenderName, VenderNo, VenderPhone,VenderMobile, "+
                "BuyerDepartId, BuyerDepartName, BuyerUserId, BuyerUserName, BuyerId, "+
                "BuyerName, BuyerNo, BuyerPhone,BuyerMobile, VRealCommision, BRealCommision, VCommision, BCommision, "+
                "VConsulting, BConsulting, Status, CreateUserId, CreateUserName, CreateTime, "+
                "BusinessDescribe, ApplicationDay, DoneDay, BudinessCode, VStandardCommision,BStandardCommision,"+
                "dbo.f_getpointallot('Depart',RecordId,'10') AS V_Departs," +
                "dbo.f_getpointallot('Employee',RecordId,'10') as V_Users," +
                "dbo.f_getpointallot('Point',RecordId,'10') as V_Points," +
                "dbo.f_getpointallot('Depart',RecordId,'20') AS B_Departs," +
                "dbo.f_getpointallot('Employee',RecordId,'20') as B_Users," +
                "dbo.f_getpointallot('Point',RecordId,'20') as B_Points," +
                "dbo.f_getpointallot('Depart',RecordId,'All') AS Departs," +
                "dbo.f_getpointallot('Employee',RecordId,'All') as Users," +
                "dbo.f_getpointallot('Point',RecordId,'All') as Points,CustomerSource";
            
            //所要查询的表名
            string TableName = "t_Contract_ApplyInfo";
            //根据sql执行查询
            return Chenxin.Lib.Business.DAL.ContractApplyInfo.GetList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, FieldList, TableFieldList, true);
        }

        /// <summary>
        /// 审核申请
        /// </summary>
        public static void AuditingApplyInfo(Chenxin.Lib.Business.Entity.ContractApplyInfo model, Guid CompanyId, int BusinessType)
        {
            //生成成交编号
            model.BudinessCode = Chenxin.Lib.Business.DAL.ContractApplyInfo.GenerateCode(CompanyId, BusinessType);
            Chenxin.Lib.Business.DAL.ContractApplyInfo.Update(model);
        }

        /// <summary>
        /// 备案查询
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_MarkedInfo> getMakrList(Guid houseId)
        {
            //生成成交编号
            string where = string.Format(" AND (HouseId = '{0}') ", houseId);
            return Chenxin.Lib.House.DAL.House_MarkedInfo.GetListArray(where);
        }

        public static DataSet GetAllPointOrBuyerAndVenderPoint(bool tag, Guid recordId)
        {
            DataSet ds = null;
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from (select distinct RecordId from t_Contract_ApplyInfo)A outer apply (select ");
            if (tag)
            {
                //上家
                sb.Append("V_Departs = dbo.f_getpointallot('Depart','t_Apply_Point_Allot',A.RecordId,'10'),");
                sb.Append("V_Users = dbo.f_getpointallot('Employee','t_Apply_Point_Allot',A.RecordId,'10'),");
                sb.Append("V_Points = dbo.f_getpointallot('Point','t_Apply_Point_Allot',A.RecordId,'10'),");
                //下家
                sb.Append("B_Departs = dbo.f_getpointallot('Depart','t_Apply_Point_Allot',A.RecordId,'20'),");
                sb.Append("B_Users = dbo.f_getpointallot('Employee','t_Apply_Point_Allot',A.RecordId,'20'),");
                sb.Append("B_Points = dbo.f_getpointallot('Point','t_Apply_Point_Allot',A.RecordId,'20')");
            }
            else
            {
                //总计
                sb.Append("Departs = dbo.f_getpointallot('Depart','t_Apply_Point_Allot',A.RecordId,'All'),");
                sb.Append("Users = dbo.f_getpointallot('Employee','t_Apply_Point_Allot',A.RecordId,'All'),");
                sb.Append("Points = dbo.f_getpointallot('Point','t_Apply_Point_Allot',A.RecordId,'All')"); 
            }
            sb.Append(")B ");
            sb.AppendFormat("where (A.RecordId = '{0}')", recordId);

            return ds = Chenxin.Lib.Business.DAL.ContractApplyInfo.ExecuteSQL(sb.ToString());
        }

        /// <summary>
        /// 根据相关内容删除表字段
        /// </summary>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public static bool DeleteApplyPointAllot(Guid recordId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from t_Apply_Point_Allot ");
            sb.AppendFormat(" where ContractId = '{0}' ", recordId);

            return Chenxin.Lib.Business.DAL.ApplyPointAllot.ExecuteSQL(sb.ToString());
        }


        /// <summary>
        /// 根据部门ID 得到该部门店长、高经、区经、商经
        /// </summary>
        /// <param name="departId"></param>
        /// <returns></returns>
        public static DataSet GetRoleByDepartId(Guid departId)
        {
            DataSet ds = null;
            StringBuilder sb = new StringBuilder();
            //店长
            sb.AppendFormat("select top 1 username,userid,'isroleb' as type from dbo.Struct_UserDepartRelation where departid='{0}' and isbelong=1 and isroleb=1 ",departId);
            sb.Append(" union ");
            //高经
            sb.AppendFormat("select top 1 username,userid,'isrolec' as type from dbo.Struct_UserDepartRelation where departid='{0}' and isbelong=1 and isrolec=1", departId);
            sb.Append(" union ");
            //区经
            sb.AppendFormat("select top 1 username,userid,'isroled' as type from dbo.Struct_UserDepartRelation where departid='{0}' and isbelong=1 and isroled=1", departId);
            sb.Append(" union ");
            //商经
            sb.AppendFormat("select top 1 username,userid,'isrolee' as type from dbo.Struct_UserDepartRelation where departid='{0}' and isbelong=1 and isrolee=1", departId);

            return ds = Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.GetTable(sb.ToString());
        }

    }
}
